﻿using System;
using System.Text;
using YanZhiwei.DotNet2.Utilities.Enums;

namespace YanZhiwei.DotNet2.Utilities.Common
{

    /// <summary>
    ///byte 帮助类
    /// </summary>
    public class ByteHelper
    {
        #region 某数除在255所占百分比
        /// <summary>
        /// 某数除在255所占百分比
        /// <para>eg:ByteHelper.MaxValueToPercent(162);==>63.53</para>
        /// </summary>
        /// <param name="value">数字，需要小于255</param>
        /// <returns>百分比</returns>
        public static decimal MaxValueToPercent(decimal value)
        {
            decimal _result = value / 255.00m;
            return Math.Round(_result * 100, 2);
        }
        #endregion
        #region 将byte数组转换成十六进制字符串
        /// <summary>
        /// 将byte数组转换成十六进制字符串
        /// <para>eg:ByteHelper.ToHexString(new byte[2] { 255, 254 },ToHexadecimal.ConvertAll);==>"FFFE"</para>
        /// </summary>
        /// <param name="bytes">byte数组</param>
        /// <param name="type">转换类型枚举</param>
        /// <returns>十六进制字符串</returns>
        public static string ToHexString(byte[] bytes, ToHexadecimal type)
        {
            string _toHexString = string.Empty;
            switch (type)
            {
                case ToHexadecimal.Loop:
                    _toHexString = ToHexStringByLoop(bytes);
                    break;
                case ToHexadecimal.BitConverter:
                    _toHexString = ToHexStringByBitConverter(bytes);
                    break;
                case ToHexadecimal.ConvertAll:
                    _toHexString = ToHexStringByConvertAll(bytes);
                    break;

            }
            return _toHexString;
        }
        private static string ToHexStringByConvertAll(byte[] bytes)
        {
            return String.Concat(Array.ConvertAll(bytes, x => x.ToString("X2")));
        }
        private static string ToHexStringByBitConverter(byte[] bytes)
        {
            string _toHexString = BitConverter.ToString(bytes);
            return _toHexString.Replace("-", "");
        }
        private static string ToHexStringByLoop(byte[] bytes)
        {
            StringBuilder _hex = new StringBuilder(bytes.Length * 2);
            foreach (byte b in bytes)
                _hex.AppendFormat("{0:X2}", b);
            return _hex.ToString();
        }
        #endregion
        #region  将byte数组转换成十六进制字符串_保留空格
        /// <summary>
        /// 将byte数组转换成十六进制字符串_保留空格
        /// <para>eg:ByteHelper.ToHexStringWithBlank(new byte[2] { 255, 254 });==>"FF FE"</para>
        /// </summary>
        /// <param name="bytes">byte数组</param>
        /// <returns>十六进制字符串</returns>
        public static string ToHexStringWithBlank(byte[] bytes)
        {
            string _toHexString = BitConverter.ToString(bytes);
            return _toHexString.Replace("-", " ");
        }
        #endregion
        #region 将byte数组转换成十六进制字符串_自定义间隔符号
        /// <summary>
        /// 将byte数组转换成十六进制字符串_自定义间隔符号
        /// <para>eg:ByteHelper.ToHexStrWithDelimiter(new byte[2] { 255, 254 },"-");==>"FF-FE"</para>
        /// </summary>
        /// <param name="bytes">byte数组</param>
        /// <param name="delimiter">自定义间隔符号</param>
        /// <returns>十六进制字符串</returns>
        public static string ToHexStrWithDelimiter(byte[] bytes, string delimiter)
        {
            string returnStr = "";
            if (bytes != null)
            {
                for (int i = 0; i < bytes.Length; i++)
                {
                    returnStr += bytes[i].ToString("X2");
                    if (i != bytes.Length - 1)
                    {
                        returnStr += delimiter;
                    }
                }
            }
            return returnStr;
        }
        #endregion
        #region 将十六进制字符串转换成byte数组
        /// <summary>
        /// 将十六进制字符串转换成byte数组
        /// <para>ByteHelper.ConvertHexString("FFFE");==>new byte[2] { 0xFF, 0xFE }</para>
        /// </summary>
        /// <param name="hex">十六进制字符串</param>
        /// <returns>byte数组</returns>
        public static byte[] ConvertHexString(string hex)
        {
            hex = hex.Replace(" ", "");
            int _hexLen = hex.Length;
            byte[] _bytes = new byte[_hexLen / 2];
            for (int i = 0; i < _hexLen; i += 2)
                _bytes[i / 2] = Convert.ToByte(hex.Substring(i, 2), 16);// byte.Parse(hex.Substring(i, 2), NumberStyles.HexNumber, CultureInfo.InvariantCulture);//
            return _bytes;
        }
        #endregion
        #region 将16进制字符串(ex: E4 CA B2) 转换成字节数组;可自定义分隔符"-" 例如00-03-00-B2-04-E6-CF
        /// <summary>
        /// 将16进制字符串(ex: E4 CA B2) 转换成字节数组;可自定义分隔符"-" 例如00-03-00-B2-04-E6-CF
        /// <para>eg:ByteHelper.ConvertHexStringWithDelimiter("00-03-00-B2", "-");==>new byte[4] { 0x00, 0x03,0x00,0xB2 }</para>
        /// </summary>
        /// <param name="hex">十六进制字符串</param>
        /// <param name="delimiter">自定义分隔符</param>
        /// <returns>byte数组</returns>
        public static byte[] ConvertHexStringWithDelimiter(string hex, string delimiter)
        {
            hex = hex.Replace(delimiter, "");
            byte[] buffer = new byte[hex.Length / 2];
            for (int i = 0; i < hex.Length; i += 2)
                buffer[i / 2] = Convert.ToByte(hex.Substring(i, 2), 16);
            return buffer;
        }
        #endregion
        #region 根据DateTime 返回7个字节的数组(yyyyMMddHHmmss)
        /// <summary>
        /// 根据DateTime 返回7个字节的数组(yyyyMMddHHmmss)
        /// <para>eg:ByteHelper.DTToBytesLen7(new DateTime(2015, 1, 05, 12, 12, 12));==>new byte[7] { 0x20, 0x15, 0x01, 0x05, 0x12, 0x12, 0x12 }</para>
        /// </summary>
        /// <param name="date">DateTime</param>
        /// <returns>byte数组</returns>
        public static Byte[] DTToBytesLen7(DateTime date)
        {
            Byte[] _bytes = new Byte[7];
            _bytes[6] = Convert.ToByte(date.Second.ToString(), 16);
            _bytes[5] = Convert.ToByte(date.Minute.ToString(), 16);
            _bytes[4] = Convert.ToByte(date.Hour.ToString(), 16);
            _bytes[3] = Convert.ToByte(date.Day.ToString(), 16);
            _bytes[2] = Convert.ToByte(date.Month.ToString(), 16);
            _bytes[1] = Convert.ToByte((date.Year.ToString().Substring(2, 2)).ToString(), 16);
            _bytes[0] = Convert.ToByte((date.Year.ToString().Substring(0, 2)).ToString(), 16);
            return _bytes;
        }
        #endregion
        #region 根据DateTime 返回六个字节的数组(yyyyMMddHHmm)
        /// <summary>
        /// 根据DateTime 返回六个字节的数组(yyyyMMddHHmm)
        /// <para>eg:ByteHelper.DTToBytesLen6(new DateTime(2015, 1, 05, 12, 12, 12));==>new byte[6] { 0x20, 0x15, 0x01, 0x05, 0x12, 0x12}</para>
        /// </summary>
        /// <param name="date">DateTime</param>
        /// <returns>byte数组</returns>
        public static Byte[] DTToBytesLen6(DateTime date)
        {
            Byte[] _bytes = new Byte[6];
            _bytes[5] = Convert.ToByte(date.Minute.ToString(), 16);
            _bytes[4] = Convert.ToByte(date.Hour.ToString(), 16);
            _bytes[3] = Convert.ToByte(date.Day.ToString(), 16);
            _bytes[2] = Convert.ToByte(date.Month.ToString(), 16);
            _bytes[1] = Convert.ToByte((date.Year.ToString().Substring(2, 2)).ToString(), 16);
            _bytes[0] = Convert.ToByte((date.Year.ToString().Substring(0, 2)).ToString(), 16);
            return _bytes;
        }
        #endregion
        #region 将时间类型转换成四个长度的byte数组 eg:2012/05/13==>{0x20,0x12,0x05,0x13}
        /// <summary>
        /// 将时间类型转换成四个长度的byte数组
        ///<para>eg:ByteHelper.DTToBytesLen4(new DateTime(2015, 1, 05, 12, 12, 12));==>new byte[4] { 0x20, 0x15, 0x01, 0x05}</para> 
        /// </summary>
        /// <param name="date">DateTime</param>
        /// <returns>byte数组</returns>
        public static byte[] DTToBytesLen4(DateTime date)
        {
            Byte[] _bytes = new Byte[4];
            _bytes[0] = Convert.ToByte((date.Year.ToString().Substring(0, 2)).ToString(), 16);
            _bytes[1] = Convert.ToByte((date.Year.ToString().Substring(2, 2)).ToString(), 16);
            _bytes[2] = Convert.ToByte(date.Month.ToString(), 16);
            _bytes[3] = Convert.ToByte(date.Day.ToString(), 16);
            return _bytes;
        }
        #endregion
        #region 将时间转换为两个长度的byte数组 eg:17：46==>{0x17,0x46}
        /// <summary>
        /// 将时间转换为两个长度的byte数组
        ///<para>eg:ByteHelper.TimeToBytesLen2("14:56");==>new byte[2] { 0x14, 0x56 }</para>
        /// </summary>
        /// <param name="time">eg: 17:46</param>
        /// <returns>byte[]</returns>
        public static byte[] TimeToBytesLen2(string time)
        {
            byte[] _bytes = new byte[2];
            if (!string.IsNullOrEmpty(time) && time.IndexOf(':') != -1)
            {
                string _hour = time.Substring(0, 2);
                string _minute = time.Substring(3, 2);
                _bytes[0] = Convert.ToByte(_hour, 16);
                _bytes[1] = Convert.ToByte(_minute, 16);
            }
            return _bytes;
        }
        #endregion
        #region 根据DateTime 返回7个字节的数组(ssmmHHMMYYYY)
        /// <summary>
        /// 根据DateTime 返回7个字节的数组(ssmmHHMMYYYY)
        /// <para>eg:ByteHelper.DTToBytesLen7Inverted(new DateTime(2015, 1, 05, 12, 12, 12));==>new byte[7] { 0x12, 0x12, 0x12, 0x05, 0x01, 0x15, 0x20 }</para>
        /// </summary>
        /// <param name="date">DateTime</param>
        /// <returns>Byte[]</returns>
        public static Byte[] DTToBytesLen7Inverted(DateTime date)
        {
            Byte[] _bytes = new Byte[7];
            _bytes[0] = Convert.ToByte(date.Second.ToString(), 16);
            _bytes[1] = Convert.ToByte(date.Minute.ToString(), 16);
            _bytes[2] = Convert.ToByte(date.Hour.ToString(), 16);
            _bytes[3] = Convert.ToByte(date.Day.ToString(), 16);
            _bytes[4] = Convert.ToByte(date.Month.ToString(), 16);
            _bytes[5] = Convert.ToByte((date.Year.ToString().Substring(2, 2)).ToString(), 16);
            _bytes[6] = Convert.ToByte((date.Year.ToString().Substring(0, 2)).ToString(), 16);
            return _bytes;
        }
        #endregion
        #region 根据DateTime 返回6个字节的数组(ssmmHHMMYY)
        /// <summary>
        /// 根据DateTime 返回6个字节的数组(ssmmHHMMYY)
        /// <para>eg:ByteHelper.DTToBytesLen6Inverted(new DateTime(2015, 1, 05, 12, 12, 12));==>new byte[6] {  0x12,0x12, 0x12, 0x05, 0x01, 0x15 }</para>
        /// </summary>
        /// <param name="date">DateTime</param>
        /// <returns>Byte[]</returns>
        public static Byte[] DTToBytesLen6Inverted(DateTime date)
        {
            Byte[] _bytes = new Byte[6];
            _bytes[0] = Convert.ToByte(date.Second.ToString(), 16);
            _bytes[1] = Convert.ToByte(date.Minute.ToString(), 16);
            _bytes[2] = Convert.ToByte(date.Hour.ToString(), 16);
            _bytes[3] = Convert.ToByte(date.Day.ToString(), 16);
            _bytes[4] = Convert.ToByte(date.Month.ToString(), 16);
            _bytes[5] = Convert.ToByte((date.ToString("yy")).ToString(), 16);
            return _bytes;
        }
        #endregion
        #region index从0开始，获取取第index位
        /// <summary>
        /// index从0开始，获取取第index位 
        /// <para>eg:ByteHelper.GetBit(8,3);==>1</para>
        /// <para>说明：8==>二进制：0001000，那么第三位即:1</para>
        /// </summary>
        /// <param name="b">操作byte</param>
        /// <param name="index">索引</param>
        /// <returns>byte</returns>
        public static int GetBit(byte b, int index)
        {
            return ((b & (1 << index)) > 0) ? 1 : 0;
        }
        #endregion
        #region  将第index位设为1
        /// <summary>
        /// 将第index位设为1
        /// <para>eg: ByteHelper.SetBit(8, 4);==>24</para>
        /// </summary>
        /// <param name="b">操作byte</param>
        /// <param name="index">索引</param>
        /// <returns>新的byte</returns>
        public static byte SetBit(byte b, int index)
        {
            return (byte)(b | (1 << index));
        }
        #endregion
        #region 将第index位设为0
        /// <summary>
        /// 将第index位设为0
        /// <para>eg:ByteHelper.ClearBit(24, 4);==>3</para>
        /// </summary>
        /// <param name="b">操作byte</param>
        /// <param name="index">索引</param>
        /// <returns>新的byte</returns>
        public static byte ClearBit(byte b, int index)
        {
            return (byte)(b & (byte.MaxValue - (1 << index)));
        }
        #endregion
        #region  将第index位取反
        /// <summary>
        ///将第index位取反
        ///<para>eg:ByteHelper.ReverseBit(24, 4);==>8</para>
        /// </summary>
        /// <param name="b">操作byte</param>
        /// <param name="index">索引</param>
        /// <returns>新的byte</returns>
        public static byte ReverseBit(byte b, int index)
        {
            return (byte)(b ^ (byte)(1 << index));
        }
        #endregion
        #region 将二进制字符串转换成byte eg：11111111==>255;00000010==>2
        /// <summary>
        /// 将二进制字符串转换成byte
        ///<para>eg:ByteHelper.BinaryToByte("11111111");==>255</para>
        /// </summary>
        /// <param name="binary">需要转换的二进制字符串【最多八个长度】</param>
        /// <returns>byte</returns>
        public static byte BinaryToByte(string binary)
        {
            return Convert.ToByte(binary, 2);
        }
        #endregion

        #region 将十六进制byte数组高地位呼唤后，转成十进制数字
        /// <summary>
        /// 将十六进制byte数组高地位呼唤后，转成十进制数字
        /// 若不能成功转换，则返回-1;
        /// <para>eg:ByteHelper.BytesToIntExHL(new byte[2] { 0xff, 0xfe }, 0, 2);==>65279</para>
        /// </summary>
        /// <param name="bytes">十六进制byte数组</param>
        /// <param name="startIndex">开始位置</param>
        /// <param name="endIndex">结束位置</param>
        /// <returns>十进制数字</returns>
        public static int BytesToIntExHL(byte[] bytes, int startIndex, int endIndex)
        {
            byte[] _bytes = ArrayHelper.Copy<byte>(bytes, startIndex, endIndex);
            if (_bytes != null)
            {
                Array.Reverse(_bytes, 0, _bytes.Length);//高地位互换
                string _hex = ToHexString(_bytes, ToHexadecimal.Loop);//转换为十六进制
                return HexHelper.ToInt(_hex);//将十六进制转换成十进制
            }
            return -1;
        }
        #endregion
        #region 将byte数组截取，高低互换后，转换成十六进制字符串
        /// <summary>
        /// 将byte数组截取，高低互换后，转换成十六进制字符串
        /// <para>eg:ByteHelper.BytesToHexExHL(new byte[2] { 0xff, 0xfe }, 0, 2);==>FEFF</para>
        /// </summary>
        /// <param name="bytes">byte数组</param>
        /// <param name="startIndex">截取起始位置</param>
        /// <param name="endIndex">截取末尾位置</param>
        /// <returns>十六进制字符串</returns>
        public static string BytesToHexExHL(byte[] bytes, int startIndex, int endIndex)
        {
            string _hex = string.Empty;
            byte[] _bytes = ArrayHelper.Copy<byte>(bytes, startIndex, endIndex);
            if (_bytes != null)
            {
                Array.Reverse(_bytes, 0, _bytes.Length);//高地位互换
                _hex = ToHexString(_bytes, ToHexadecimal.Loop);//转换为十六进制
            }
            return _hex;
        }
        #endregion
        #region 将Byte数组转换成时间格式
        /// <summary>
        ///根据包里的用户数据(7或6字节)解析时间，返回DateTime 
        ///<para>eg: ByteHelper.BytesInvertedToDT(new byte[7] { 0x12, 0x12, 0x12, 0x05, 0x01, 0x15, 0x20 },0,7);==>new DateTime(2015, 1, 05, 12, 12, 12)</para>
        /// </summary>
        /// <param name="bytes">操作数组</param>
        /// <param name="startIndex">截取开始位置</param>
        /// <param name="endIndex">截取末尾位置</param>
        /// <returns>DateTime</returns>
        public static DateTime BytesInvertedToDT(Byte[] bytes, int startIndex, int endIndex)
        {
            byte[] _bytes = ArrayHelper.Copy<byte>(bytes, startIndex, endIndex);
            //按照格式，时间数据应为6个字节
            if (_bytes != null && (_bytes.Length == 6 || _bytes.Length == 7))
            {
                int second = Convert.ToInt16(_bytes[0].ToString("X2"));
                int minute = Convert.ToInt16(_bytes[1].ToString("X2"));
                int hour = Convert.ToInt16(_bytes[2].ToString("X2"));
                int day = Convert.ToInt16(_bytes[3].ToString("X2"));
                int month = Convert.ToInt16(_bytes[4].ToString("X2"));
                int year = 2000 + Convert.ToInt16(_bytes[5].ToString("X2"));
                return new DateTime(year, month, day, hour, minute, second);
            }
            else
            {
                throw new Exception("时间格式应该6或7个字节！错误:" + _bytes.Length.ToString());
            }
        }
        #endregion
        #region YYYYMMDD MMHH的byte数组转换对应的时间类型
        /// <summary>
        /// YYYYMMDD MMHH转换对应的时间类型
        /// <para>eg:ByteHelper.BytesLen6ToDT(new byte[7] { 0x20, 0x15, 0x01, 0x05, 0x12, 0x12, 0x12 }, 0, 6);==>new DateTime(2015, 01, 05, 12, 12, 00)</para>
        /// </summary>
        /// <param name="bytes">操作数组</param>
        /// <param name="startIndex">截取起始位置</param>
        /// <param name="endIndex">截取末尾位置</param>
        /// <returns>DateTime</returns>
        public static DateTime BytesLen6ToDT(Byte[] bytes, int startIndex, int endIndex)
        {
            byte[] _bytes = ArrayHelper.Copy<byte>(bytes, startIndex, endIndex);
            //按照格式，时间数据应为6个字节
            if (_bytes != null && _bytes.Length == 6)
            {
                int minute = Convert.ToInt16(_bytes[5].ToString("X2"));
                int hour = Convert.ToInt16(_bytes[4].ToString("X2"));
                int day = Convert.ToInt16(_bytes[3].ToString("X2"));
                int month = Convert.ToInt16(_bytes[2].ToString("X2"));
                int year = Convert.ToInt16(Convert.ToInt16(_bytes[0].ToString("X2")).ToString() + Convert.ToInt16(_bytes[1].ToString("X2")).ToString());
                return new DateTime(year, month, day, hour, minute, 0);
            }
            else
            {
                throw new Exception("时间格式应该6个字节！错误:" + bytes.Length.ToString());
            }
        }
        #endregion
        #region YYYYMMDD MMHH的byte数组转换对应的时间类型
        /// <summary>
        /// YYYYMMDD MMHH转换对应的时间类型
        /// <para>eg:ByteHelper.BytesLen7ToDT(new byte[7] { 0x20, 0x15, 0x01, 0x05, 0x12, 0x12, 0x12 }, 0, 7);==>new DateTime(2015, 01, 05, 12, 12, 12)</para>
        /// </summary>
        /// <param name="bytes">操作数组</param>
        /// <param name="startIndex">截取起始位置</param>
        /// <param name="endIndex">截取末尾位置</param>
        /// <returns>DateTime</returns>
        public static DateTime BytesLen7ToDT(Byte[] bytes, int startIndex, int endIndex)
        {
            if (endIndex - startIndex != 7)
                throw new ArgumentException(string.Format("{0}与{1}相差不等于7", startIndex, endIndex));
            byte[] _bytes = ArrayHelper.Copy<byte>(bytes, startIndex, endIndex);
            //按照格式，时间数据应为6个字节
            if (_bytes != null && _bytes.Length == 7)
            {
                int second = Convert.ToInt16(_bytes[6].ToString("X2"));
                int minute = Convert.ToInt16(_bytes[5].ToString("X2"));
                int hour = Convert.ToInt16(_bytes[4].ToString("X2"));
                int day = Convert.ToInt16(_bytes[3].ToString("X2"));
                int month = Convert.ToInt16(_bytes[2].ToString("X2"));
                int year = Convert.ToInt16(Convert.ToInt16(_bytes[0].ToString("X2")).ToString() + Convert.ToInt16(_bytes[1].ToString("X2")).ToString());
                return new DateTime(year, month, day, hour, minute, second);
            }
            else
            {
                throw new Exception("时间格式应该7个字节！错误:" + bytes.Length.ToString());
            }
        }
        #endregion
        #region 将4个长度的Byte数组转换成时间类型
        /// <summary>
        /// 将4个长度的Byte数组转换成时间类型
        ///<para>eg:ByteHelper.BytesLen4ToDT(new byte[4] { 0x20, 0x12, 0x10, 0x12 }, 0, 4);==>new DateTime(2012, 10, 12)</para> 
        /// </summary>
        /// <param name="bytes">需要转换的byte数组</param>
        /// <param name="startIndex">截取的起始位</param>
        /// <param name="endIndex">截取的终止位</param>
        /// <returns>转换的时间</returns>
        public static DateTime BytesLen4ToDT(byte[] bytes, int startIndex, int endIndex)
        {
            DateTime _time = default(DateTime);
            byte[] _bytes = ArrayHelper.Copy<byte>(bytes, startIndex, endIndex);
            if (_bytes != null && _bytes.Length == 4)
            {
                int _year = Convert.ToInt16(Convert.ToInt16(_bytes[0].ToString("X2")).ToString() + Convert.ToInt16(_bytes[1].ToString("X2")).ToString());
                int _month = Convert.ToInt16(_bytes[2].ToString("X2"));
                int _day = Convert.ToInt16(_bytes[3].ToString("X2"));
                _time = new DateTime(_year, _month, _day);
            }
            return _time;
        }
        #endregion
        #region 将byte数组[截取后两位长度]转换成时间[HH:ss格式]
        /// <summary>
        ///将byte数组[截取后两位长度]转换成时间[HH:ss格式]
        ///<para>eg:ByteHelper.BytesLen2ToTime(new byte[2] { 0x17, 0x45 }, 0, 2);==>"17:45"</para>
        /// </summary>
        /// <param name="bytes">需要转换的byte数组</param>
        /// <param name="startIndex">截取的起始位</param>
        /// <param name="endIndex">截取的终止位</param>
        /// <returns>转换的时间</returns>
        public static string BytesLen2ToTime(byte[] bytes, int startIndex, int endIndex)
        {
            string _time = string.Empty;
            byte[] _bytes = ArrayHelper.Copy<byte>(bytes, startIndex, endIndex);
            if (_bytes != null && _bytes.Length == 2)
            {
                int _hour = Convert.ToInt16(_bytes[0].ToString("X2"));
                int _minute = Convert.ToInt16(_bytes[1].ToString("X2"));
                _time = string.Format("{0}:{1}", _hour, _minute);
            }
            return _time;
        }
        #endregion
        #region 将byte数组转换成十六进制字符串
        /// <summary>
        /// 将byte数组转换成十六进制字符串
        /// <para>eg:ByteHelper.BytesToHexString(new byte[2] { 255, 254 }, 0, 2);==>"FFFE"</para>
        /// </summary>
        /// <param name="bytes">操作byte数组</param>
        /// <param name="startIndex">截取的起始位</param>
        /// <param name="endIndex">截取的终止位</param>
        /// <returns>十六进制字符串</returns>
        public static string BytesToHexString(byte[] bytes, int startIndex, int endIndex)
        {
            byte[] _bytes = ArrayHelper.Copy<byte>(bytes, startIndex, endIndex);
            return ToHexString(_bytes, ToHexadecimal.BitConverter);

        }
        #endregion
        #region 将byte数组转换成十六进制带空格的字符串
        /// <summary>
        ///将byte数组转换成十六进制带空格的字符串
        ///<para>eg:ByteHelper.ByteToHexWithBlank(new byte[2] { 255, 254 }, 0, 2);==>FF FE</para>
        /// </summary>
        /// <param name="bytes">操作byte数组</param>
        /// <param name="startIndex">截取的起始位</param>
        /// <param name="endIndex">截取的终止位</param>
        /// <returns>十六进制字符串带空格</returns>
        public static string ByteToHexWithBlank(byte[] bytes, int startIndex, int endIndex)
        {
            byte[] _bytes = ArrayHelper.Copy<byte>(bytes, startIndex, endIndex);
            return ToHexStringWithBlank(_bytes);
        }
        #endregion
        #region 将byte数组转换Int
        /// <summary>
        /// 将byte数组转换Int
        /// <para>eg:ByteHelper.BytesToInt(new byte[2] { 255, 254 }, 0, 2);==>65534</para>
        /// </summary>
        /// <param name="bytes">操作byte数组</param>
        /// <param name="startIndex">截取的起始位</param>
        /// <param name="endIndex">截取的终止位</param>
        /// <returns>int</returns>
        public static int BytesToInt(byte[] bytes, int startIndex, int endIndex)
        {
            byte[] _bytes = ArrayHelper.Copy<byte>(bytes, startIndex, endIndex);
            string _hex = ToHexString(_bytes, ToHexadecimal.BitConverter);
            return HexHelper.ToInt(_hex);
        }
        #endregion
        #region 将Int转换成Byte
        /// <summary>
        /// 将Int转换成Byte
        /// <para>eg：ByteHelper.ToByte(254);==》254</para>
        /// </summary>
        /// <param name="value">int</param>
        /// <returns>Byte</returns>
        public static byte ToByte(int value)
        {
            string _hexStr = HexHelper.ToHexString(value);
            return Convert.ToByte(_hexStr, 16);
        }
        #endregion
        #region 将十六进制百分比(0~255)转换成Byte
        /// <summary>
        /// 将十六进制百分比(0~255)转换成Byte
        /// <para>eg:ByteHelper.PercentToByte(70);==>178</para>
        /// </summary>
        /// <param name="percent">int</param>
        /// <returns>byte</returns>
        public static byte PercentToByte(int percent)
        {
            string _percentHex = HexHelper.PercentToHex(percent, 2);
            return Convert.ToByte(_percentHex, 16);
        }
        #endregion
        #region 将int转换成byte数组
        /// <summary>
        /// 将int转换成byte数组
        /// <para>eg:ByteHelper.ToBytes(258);==>new byte[2] { 0x01, 0x02 }</para>
        /// </summary>
        /// <param name="value">int</param>
        /// <returns>byte数组</returns>
        public static byte[] ToBytes(int value)
        {
            string _hex = HexHelper.ToHexString(value);
            byte[] _bytes = ConvertHexString(_hex);
            return _bytes;
        }
        #endregion
        #region 将十六进制字符串转换成byte数组，然后高低位互换
        /// <summary>
        /// 将十六进制字符串转换成byte数组，然后高低位互换
        /// <para>eg: ByteHelper.ToBytesExHL("FEFF");==>new byte[2] { 0xFF, 0xFE }</para>
        /// </summary>
        /// <param name="hexString">十六进制字符串</param>
        /// <returns>byte数组</returns>
        public static byte[] ToBytesExHL(string hexString)
        {
            byte[] _bytes = ConvertHexString(hexString);
            Array.Reverse(_bytes, 0, _bytes.Length);
            return _bytes;
        }
        #endregion
        #region 将int转换byte数组，然后高低位互换
        /// <summary>
        /// 将int转换byte数组，然后高低位互换
        /// <para>eg:ByteHelper.ToBytesExHL(258);==>new byte[2] { 0x02, 0x01 }</para>
        /// </summary>
        /// <param name="value">int</param>
        /// <returns>byte数组</returns>
        public static byte[] ToBytesExHL(int value)
        {
            string _hex = HexHelper.ToHexString(value);
            byte[] _bytes = ConvertHexString(_hex);
            Array.Reverse(_bytes, 0, _bytes.Length);
            return _bytes;
        }
        #endregion
        #region 将int转换两个长度byte数组，然后高低位互换
        /// <summary>
        /// 将int转换两个长度byte数组，然后高低位互换
        /// <para>eg:ByteHelper.ToBytesLen2ExHL(258);==>new byte[2] { 0x02, 0x01 }</para>
        /// </summary>
        /// <param name="value">int</param>
        /// <returns>byte数组</returns>
        public static byte[] ToBytesLen2ExHL(int value)
        {
            string _hex = HexHelper.ToHexString(value);
            byte[] _bytes = new byte[2];
            byte[] _byteTmp = ConvertHexString(_hex);
            if (_byteTmp.Length < 2)
            {
                _byteTmp.CopyTo(_bytes, 0);
                return _bytes;
            }
            else
            {
                Array.Reverse(_byteTmp, 0, _byteTmp.Length);
                return _byteTmp;
            }
        }
        #endregion
        #region  将二进制字符串逆转，低位在前高位在后，然后转换成十六进制byte
        /// <summary>
        /// 将二进制字符串逆转，低位在前高位在后，然后转换成十六进制byte
        /// <para>eg:ByteHelper.ReverseBinaryToByte("11110000");==>15</para>
        /// </summary>
        /// <param name="binaryStr">二进制字符串</param>
        /// <returns>byte</returns>
        public static byte ReverseBinaryToByte(string binaryStr)
        {
            return Convert.ToByte(StringHelper.Reverse(binaryStr), 2);
        }
        #endregion
        #region  若source大于compare，则取source反。
        /// <summary>
        /// 若source大于compare，则取source反。
        /// <para>eg:ByteHelper.OverPointValueUseReversed(new byte[2] { 0xff, 0xfe }, new byte[2] { 0xff, 0xff });==>65534</para>
        /// </summary>
        /// <param name="source">byte数组</param>
        /// <param name="compare">byte数组</param>
        /// <returns>int</returns>
        public static int OverPointValueUseReversed(byte[] source, byte[] compare)
        {
            int _resultValue = 0;

            string _sourceHex = ToHexString(source, ToHexadecimal.BitConverter);
            int _sourceIntValue = HexHelper.ToInt(_sourceHex);

            string _compareHex = ToHexString(compare, ToHexadecimal.BitConverter);
            int _compareIntValue = HexHelper.ToInt(_compareHex);

            _resultValue = _sourceIntValue > _compareIntValue == true ? ~_sourceIntValue : _sourceIntValue;
            return _resultValue;
        }
        /// <summary>
        /// 若source大于compare，则取source反。
        /// </summary>
        /// <param name="source">byte数组</param>
        /// <param name="compare">byte数组</param>
        /// <param name="divisorValue">取反后需要除的除数</param>
        /// <returns>decimal</returns>
        public static decimal OverPointValueUseReversed(byte[] source, byte[] compare, decimal divisorValue)
        {
            int _resultValue = OverPointValueUseReversed(source, compare);
            return Math.Abs(_resultValue / divisorValue);
        }
        /// <summary>
        /// 若source大于compare，则取source反。
        /// </summary>
        /// <param name="source">byte数组</param>
        /// <param name="compare">byte数组</param>
        /// <param name="divisorValue">取反后需要除的除数</param>
        /// <param name="precisionValue">除后，需要保留的精度</param>
        /// <returns>decimal</returns>
        public static decimal OverPointValueUseReversed(byte[] source, byte[] compare, decimal divisorValue, int precisionValue)
        {
            decimal _resultValue = OverPointValueUseReversed(source, compare, divisorValue);
            return Math.Round(_resultValue, precisionValue);
        }
        #endregion
        #region 将byte数组转换成二进制字符串
        /// <summary>
        /// 将byte数组转换成二进制字符串
        ///<para>eg:ByteHelper.BytesToBinaryString(new byte[1] { 0xff }, 0, 1)==>"11111111"</para> 
        /// </summary>
        /// <param name="bytes">操作数组</param>
        /// <param name="startIndex">截取开始位置</param>
        /// <param name="endIndex">截取末尾位置</param>
        /// <returns>二进制字符串</returns>
        public static string BytesToBinaryString(byte[] bytes, int startIndex, int endIndex)
        {
            string _hexString = BytesToHexString(bytes, startIndex, endIndex);
            return Convert.ToString(HexHelper.ToInt(_hexString), 2);
        }
        #endregion
        #region 将二进制字符串转换成Byte数组 eg:"1111 1111 1111 1111"==>byte[2]{0xff,0xff};
        /// <summary>
        /// 将二进制字符串转换成Byte数组
        ///<para> eg:ByteHelper.BinaryStringToBytes("1111111111111111");==>new byte[2] { 0xff, 0xff }</para>
        /// </summary>
        /// <param name="binaryString"></param>
        /// <returns></returns>
        public static byte[] BinaryStringToBytes(String binaryString)
        {
            binaryString = binaryString.Replace(" ", "");
            int _numOfBytes = binaryString.Length / 8;
            byte[] _bytes = new byte[_numOfBytes];
            for (int i = 0; i < _numOfBytes; ++i)
            {
                _bytes[i] = Convert.ToByte(binaryString.Substring(8 * i, 8), 2);
            }
            return _bytes;
        }
        #endregion
        #region  将十六进制byte数组高地位呼唤后，转成十进制数字字符串
        /// <summary>
        /// 将十六进制byte数组高地位呼唤后，转成十进制数字字符串
        /// </summary>
        /// <param name="bytes">byte数组</param>
        /// <param name="startIndex">数组开始位置</param>
        /// <param name="endIndex">数组结束位置</param>
        /// <returns>若转换失败则返回'--'</returns>
        public static string BytesToIntStringExHL(byte[] bytes, int startIndex, int endIndex)
        {
            int _number = ByteHelper.BytesToIntExHL(bytes, startIndex, endIndex);
            return (_number != -1) ? _number.ToString() : "--";
        }
        #endregion 
    }
}
